package com.nfit.shopadmin.service.impl;
import com.nfit.shopadmin.mapper.CategoryMapper;
import com.nfit.shopadmin.mapper.ProductSkuMapper;
import com.nfit.shopadmin.service.CategoryService;
import com.nfit.shopmodel.entity.product.Category;

import com.nfit.shopmodel.entity.product.ProductSku;
import com.nfit.shopmodel.vo.category.CategoryDTO;
import com.nfit.shopmodel.vo.category.ProductDTO;
import com.nfit.shopmodel.vo.category.SkuDTO;
import com.nfit.shopmodel.vo.category.SubCategoryDTO;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * ClassName:CategoryServiceImpl
 * Package:com.nfit.shopweb.service.impl
 * Description
 *
 * @Author L
 * @Create 2024/12/9 9:57
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    /**
     * 移动端查询前10条商品分类数据
     */
    @Override
    public List<Category> getCategoryList() {
        List<Category> list = categoryMapper.getCategoryList();
        return list;
    }

    @Override
    public List<CategoryDTO> getCategoriesWithDetails() {
        // 获取所有一级分类
        List<CategoryDTO> categories = categoryMapper.findAllCategories();

        for (CategoryDTO category : categories) {
            // 获取一级分类下的二级分类
            List<SubCategoryDTO> subCategories = categoryMapper.findSubCategoriesByParentId(Long.valueOf(category.getId()));
            for (SubCategoryDTO subCategory : subCategories) {
                // 获取二级分类下的商品
                List<ProductDTO> products = categoryMapper.findProductsByCategoryId(Long.valueOf(subCategory.getId()));
                for (ProductDTO product : products) {
                    // 获取商品的价格
                    ProductSku productSku = productSkuMapper.findByProductId(product.getId());
                    product.setPrice(productSku.getSalePrice()); //封装数据
                    product.setPicture(productSku.getThumbImg());
                }
                subCategory.setGoods(products);
            }
            category.setChildren(subCategories);
        }
        return categories;
    }
}
